/****详情页指令*/
yjx.directive('openDetail', ['$rootScope', '$compile', '$timeout', function openDetail($rootScope, $compile, $timeout) {
//详情页打开指令 页面作用域为隔离作用域；
    return {
        restrict: 'A',
        priority: 1,
        scope: {//利用ng-init可单向传入数据且不影响作用域
            temp: '@template',
            param: '=param',//参数可传入页面 非对象；字符串
            submitFun: '=submitFun',//指定函数
        },
        link: function ($scope, elem, attr) {
            $timeout(function () {
                //$scope.temp = attr.template;
                //$scope.param = attr.param;
                //console.info($scope.temp);
                //console.info($scope.param);
                $rootScope._dCount = $rootScope._dCount ? $rootScope._dCount : 0;//初始化详情页(子节点)数
                $scope.temp = $scope.temp;
                //$scope.param =JSON.parse();
                //console.info($scope.param)
                elem.bind('click', function () {
                    //每次操作结束重新监听
                    $timeout(function () {
                        //    console.info($scope.contrl)
                        var html = '<div class="e-detail" ng-include="\'' + $scope.temp + '\'"></div>';
                        var detailHtml = $compile(html)($scope)
                        angular.element(document.querySelector('#m-detail')).append(detailHtml);//后排插入
                        angular.element(document.querySelector('#m-detail')).show();
                        $rootScope._dCount++; //子节点数加一
                    })
                })
            })

        }
    }
}]);
yjx.directive('openDetail2', ['$rootScope', '$compile', '$timeout', '$locals',
    function openDetail2($rootScope, $compile, $timeout, $locals) {
//详情页打开指令 页面作用域包含父作用域；
        return {
            restrict: 'A',
            priority: 1,
            link: function ($scope, elem, attr) {
                $timeout(function () {
                    $rootScope._dCount = $rootScope._dCount ? $rootScope._dCount : 0;//初始化详情页(子节点)数
                    $scope.supersede = function () {
                        if ($scope.supersedeInit) {
                            $scope.supersedeInit(); //初始化函数
                        }
                        $timeout(function () {
                            var html = '<div class="e-detail" ng-include="\'' + $scope.temp + '\'"></div>';
                            var detailHtml = $compile(html)($scope)
                            angular.element(document.querySelector('#m-detail')).append(detailHtml);//后排插入
                            angular.element(document.querySelector('#m-detail')).show();
                            $rootScope._dCount++; //子节点数加一
                        })
                    }
                    elem.bind('click', function () {
                        $scope.supersede();
                    })
                })

            }
        }
    }]);
yjx.directive('eDetail', ['$rootScope', '$timeout', function eDetail($rootScope, $timeout) {
//详情页关闭指令
    return {
        restrict: 'AEC',
        priority: 1,
        link: function ($scope, elem, attr) {
            $rootScope._dCount = $rootScope._dCount ? $rootScope._dCount : 0;//初始化详情页(子节点)数
            var cancelWatch = $scope.$watch(attr.killDetail, function (newVal, oldVal) {
                var mDetail = elem.closest('#m-detail');//父级 从当前位置向上查找第一个匹配的元素；
                var ds = mDetail.find('.e-detail');//所有e-detail
                var k = elem.find('.e-kill');//关闭触发
                //移除子节点绑定kill
                k.bind('click', function (event) {
                    if (ds.length == 1) {//只有一个e-detail子节点,隐藏父节点并清除内容
                        mDetail.hide();
                        mDetail.html('');
                        $timeout(function () {
                            $rootScope._dCount = 0
                        })
                        //return;
                    } else if ($rootScope._dCount > 0) {//移除子节点;子节点数减一
                        $timeout(function () {
                            $rootScope._dCount--;
                            elem.remove();
                        })
                    }
                    //$scope.param = '123120';
                    ////最后返回执行的方法
                    //if($scope.newsPageList){//评论更新
                    //    $scope.newsPageList('auto');
                    //}
                })
                $scope.eKill = function (event) {
                    var ek = $(event.target).closest('.e-detail');
                    if (ek.length > 0) {
                        ek.remove()
                        $rootScope._dCount--;
                    }
                }
                cancelWatch();
                return;
            })
        }
    }
}]);
yjx.directive('targetOpen', ['$rootScope', '$location', '$locals',
    function targetOpen($rootScope, $location, $locals) {
        //target-open 用于含有href的标签中 打开详情页记录当前url为历史浏览
        return {
            restrict: 'EA',
            link: function ($scope, elem, attr) {
                elem.bind('click', function () {
                    //点击时记录路由信息
                    var urlCache = $location.path();
                    $locals.set('urlCache', urlCache);
                    //console.info(urlCache)
                })
                if (attr.targetOpen) {
                    elem.attr('target', attr.targetOpen)
                } else {//默认为新窗口打开
                    elem.attr('target', '_new')
                }
            }
        }
    }])